<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="状态，0：新增，1：缓冲，2：待结算，3：已结算" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态，0：新增，1：缓冲，2：待结算，3：已结算"></el-input>
    </el-form-item>
    <el-form-item label="删除标志，0：未删除，1：删除" prop="deleted">
      <el-input v-model="dataForm.deleted" placeholder="删除标志，0：未删除，1：删除"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建者ID" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建者ID"></el-input>
    </el-form-item>
    <el-form-item label="租户ID" prop="tenantId">
      <el-input v-model="dataForm.tenantId" placeholder="租户ID"></el-input>
    </el-form-item>
    <el-form-item label="收款单主表ID" prop="receiptId">
      <el-input v-model="dataForm.receiptId" placeholder="收款单主表ID"></el-input>
    </el-form-item>
    <el-form-item label="销售订单Id" prop="salesOrderHeaderId">
      <el-input v-model="dataForm.salesOrderHeaderId" placeholder="销售订单Id"></el-input>
    </el-form-item>
    <el-form-item label="本次核销金额" prop="thisWrittenOffAmount">
      <el-input v-model="dataForm.thisWrittenOffAmount" placeholder="本次核销金额"></el-input>
    </el-form-item>
    <el-form-item label="本次核销次数" prop="thisWrittenOffNum">
      <el-input v-model="dataForm.thisWrittenOffNum" placeholder="本次核销次数"></el-input>
    </el-form-item>
    <el-form-item label="收款日期" prop="receiptTime">
      <el-input v-model="dataForm.receiptTime" placeholder="收款日期"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          status: '',
          deleted: '',
          createTime: '',
          createUserId: '',
          tenantId: '',
          receiptId: '',
          salesOrderHeaderId: '',
          thisWrittenOffAmount: '',
          thisWrittenOffNum: '',
          receiptTime: '',
          remark: '',
          salesOrderHeaderName: '',
          saleDate: '',
          totalAmount: '',
          deposit: '',
          writtenOffAmount: '',
          remainAmount: '',
          writtenOffNum: ''
        },
        dataRule: {
          status: [
            { required: true, message: '状态，0：新增，1：缓冲，2：待结算，3：已结算不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '删除标志，0：未删除，1：删除不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建者ID不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户ID不能为空', trigger: 'blur' }
          ],
          receiptId: [
            { required: true, message: '收款单主表ID不能为空', trigger: 'blur' }
          ],
          salesOrderHeaderId: [
            { required: true, message: '销售订单Id不能为空', trigger: 'blur' }
          ],
          thisWrittenOffAmount: [
            { required: true, message: '本次核销金额不能为空', trigger: 'blur' }
          ],
          thisWrittenOffNum: [
            { required: true, message: '本次核销次数不能为空', trigger: 'blur' }
          ],
          receiptTime: [
            { required: true, message: '收款日期不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/funds/receiptdetail/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.status = data.receiptDetail.status
                this.dataForm.deleted = data.receiptDetail.deleted
                this.dataForm.createTime = data.receiptDetail.createTime
                this.dataForm.createUserId = data.receiptDetail.createUserId
                this.dataForm.tenantId = data.receiptDetail.tenantId
                this.dataForm.receiptId = data.receiptDetail.receiptId
                this.dataForm.salesOrderHeaderId = data.receiptDetail.salesOrderHeaderId
                this.dataForm.thisWrittenOffAmount = data.receiptDetail.thisWrittenOffAmount
                this.dataForm.thisWrittenOffNum = data.receiptDetail.thisWrittenOffNum
                this.dataForm.receiptTime = data.receiptDetail.receiptTime
                this.dataForm.remark = data.receiptDetail.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/funds/receiptdetail/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'status': this.dataForm.status,
                'deleted': this.dataForm.deleted,
                'createTime': this.dataForm.createTime,
                'createUserId': this.dataForm.createUserId,
                'tenantId': this.dataForm.tenantId,
                'receiptId': this.dataForm.receiptId,
                'salesOrderHeaderId': this.dataForm.salesOrderHeaderId,
                'thisWrittenOffAmount': this.dataForm.thisWrittenOffAmount,
                'thisWrittenOffNum': this.dataForm.thisWrittenOffNum,
                'receiptTime': this.dataForm.receiptTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
